package com.ttl.bc.stock.request2.service;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ttl.bc.ap.invoice.bo.BCAPInvoiceDAO;
import com.ttl.bc.ap.invoice.service.BCAPInvoiceDocNoBean;

public class BCStkIssue2Service {
	
	@SuppressWarnings("unchecked")
	public BCAPInvoiceDocNoBean getMaxDocNo(String id) {

		BCAPInvoiceDocNoBean bean = new BCAPInvoiceDocNoBean();
		
		Date date = new Date();
		
		SimpleDateFormat simpleYear = new SimpleDateFormat("yyyy");
		String yearStr = simpleYear.format(date);
		
		if(Integer.valueOf(yearStr)<2500) {
			int yearStr2 = Integer.valueOf(yearStr)+543;
			yearStr = (""+yearStr2).substring(2);
		} else {
			yearStr = yearStr.substring(2);
		}
		
		SimpleDateFormat simpleMonth = new SimpleDateFormat("MM");
		@SuppressWarnings("unused")
		String monthStr = simpleMonth.format(date);
		
		String docNoStr = id;//+yearStr+monthStr;
		
		String hql = "SELECT MAX(docNo) FROM BCStkIssue2 WHERE docNo like '"+docNoStr+"%' ";

		BCAPInvoiceDAO dao = new BCAPInvoiceDAO();				
		
		List result = dao.find(hql);
		String docNo = null;
		
		String maxDocNo = null;
		if(result.get(0) != null) {
			
			docNo = result.get(0).toString();
			int temp = 1+Integer.valueOf(docNo.substring(docNo.length()-3,docNo.length()));			
			NumberFormat formatter = new DecimalFormat("000");
			docNo = formatter.format(temp);
			
			maxDocNo = docNoStr+""+docNo; 			
		}else {
			maxDocNo = docNoStr+"001";
		}
					
		bean.setDocNo(maxDocNo);
		return bean;		
		
	}
}
